package com.utils;

import java.io.*;

public class StreamUtils {

    /**
     * 1. 将输入流转换成字节数组
     *      就是将文件内容读取到一个字节数组中
     * @return
     */
    public static byte[] streamToByteArray(InputStream is) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream(); // 创建输出流
        byte[] bytes =  new byte[1024]; // 字节数组
        int len;
        while ((len=is.read(bytes))!=-1){
            bos.write(bytes, 0 ,len);
        }
        byte[] array = bos.toByteArray();
        bos.close();
        return array;
    }


    /**
     * 1. 将一个输入流中的数据，直接转换成 string
     * @param is
     * @return
     * @throws IOException
     */
    public static String streamToString(InputStream is) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
        StringBuilder builder = new StringBuilder();
        String len;
        while ((len = bufferedReader.readLine())!=null){
            builder.append(len+"\r\n");
        }
        return builder.toString();
    }
}
